package com.app.Activity;

import java.io.File;
import java.util.ArrayList;

import ReadWriteXML.CMember;
import ReadWriteXML.ReadXML;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import app.com.ModuleRole.R;

import com.app.Controller.ActivityController;
import com.app.Controller.NetworkController;
import com.app.Controller.ReadWriteFileController;
import com.app.Network.Protocol;
import com.app.PhanQuyen.Quyen;

public class frmNhomChoActivity extends Activity {

	public static String NAME_CODE = "NAME_CODE";
	public static String IP_CODE = "IP_CODE";
	public static boolean groupFull = false;
	public static Runnable raRefreshListView;
	ListView lvDanhSachThanhVien;
	Button btnMoiThanhVien, btnBatDau;
	TextView txtGroupName;
	String groupName = "";
	ArrayList<CMember> memberList;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_nhom_cho);
		lvDanhSachThanhVien = (ListView) findViewById(R.id.lvDanhSachThanhVien);
		btnMoiThanhVien = (Button) findViewById(R.id.btnMoiThanhVien);
		btnBatDau = (Button) findViewById(R.id.btnBatDau);
		txtGroupName = (TextView) findViewById(R.id.txtGroupName);

		fillListView();
		registerForContextMenu(lvDanhSachThanhVien);
		btnMoiThanhVien.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Quyen moiThanhVien = NetworkController._peer.getAccount().getRuleById(1);
				if (moiThanhVien != null && moiThanhVien.getSend() == true) {
					Intent intent = new Intent(getBaseContext(), frmDanhSachOnline.class);
					intent.putExtra(frmTaoNhom.CODE_GROUP_NAME, groupName);
					startActivity(intent);
				} else {
					Show(Protocol.NON_RULE_MESSAGE);
				}
			}
		});
		btnBatDau.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (NetworkController.getIsThanhLapNhom()) {
					Show("Bắt đầu");
				} else {
					Show(Protocol.NON_RULE_MESSAGE);
				}

			}
		});
		// Lấy tên nhóm từ activity trước
		Intent i = getIntent();
		if (i != null) {
			if (i.getStringExtra(frmTaoNhom.CODE_GROUP_NAME) != null)
				groupName = i.getStringExtra(frmTaoNhom.CODE_GROUP_NAME);
		}
		txtGroupName.setText(groupName);
		raRefreshListView = new Runnable() {
			@Override
			public void run() {
				Log.d("Phu", "frmNhomChoActivity raRefreshListView");
				fillListView();
			}
		};
	}

	private void fillListView() {
		memberList = ReadXML.ReadMember(ReadWriteFileController.MEMBER_PATH);
		ArrayAdapter<CMember> adapter = new ArrayAdapter<CMember>(this, android.R.layout.simple_list_item_1, memberList);
		lvDanhSachThanhVien.setAdapter(adapter);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		if (v.getId() == R.id.lvDanhSachThanhVien) {
			AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
			menu.setHeaderTitle("Thao tác");
			menu.addSubMenu(Menu.NONE, 1, 1, "Đặt vai trò");
			menu.addSubMenu(Menu.NONE, 2, 2, "Yêu cầu thoát");
			menu.addSubMenu(Menu.NONE, 3, 3, "Chat");
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		int menuItemIndex = item.getItemId();
		if (menuItemIndex == 1) {
			Quyen setRoleChoNguoiKhac = NetworkController._peer.getAccount().getRule(3);
			if (NetworkController.getIsThanhLapNhom() || (setRoleChoNguoiKhac != null && setRoleChoNguoiKhac.getSend())) {
				Intent intent = new Intent(this, frmSetRoleActivity.class);
				intent.putExtra(NAME_CODE, memberList.get(info.position).get_fullName());
				intent.putExtra(IP_CODE, memberList.get(info.position).get_ip());
				startActivity(intent);
			} else {
				Show(Protocol.NON_RULE_MESSAGE);
			}
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		new MenuInflater(getApplication()).inflate(R.menu.omnu_nhom_cho, menu);
		return (super.onCreateOptionsMenu(menu));
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		switch (id) {
		case R.id.mnuMyRole:
			Intent intent = new Intent(getBaseContext(), frmVaiTroCuaToiActivity.class);
			startActivity(intent);
			break;
		default:
			break;
		}
		return true;
	}

	private void readGroupMember() {
		memberList = ReadXML.ReadMember(ReadWriteFileController.MEMBER_PATH);
	}

	synchronized public static void insertMember(CMember member) {
		ReadWriteFileController.insertMember(ReadWriteFileController.MEMBER_PATH, member);
		Log.d("Phu", "insertMember trong frmNhomChoActivity");
	}

	public void Show(String message) {
		// Toast.makeText(this, "hehe Xin chao", Toast.LENGTH_SHORT).show();
		AlertDialog dlg = new AlertDialog.Builder(this).setTitle("Thông báo").setMessage(message)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
					}
				}).create();
		dlg.show();
	}

	@Override
	public void onResume() {
		super.onResume();
		fillListView();
	}
}
